﻿using System;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

using ProjectBase.Core.Publishing;
using ProjectBase.Core.Publishing.Configuration;

namespace $safeprojectname$.Infrastructure.Factories
{
    public static class PublishingConfigurationFactory
    {
        #region Read Configuration For
        public static PublishingConfiguration GetProviderWithLog4NetOnly()
        {
            var section = GetSectionExample("log4NetProvider");
            return new ConfigurationSectionHandler().Create(section);
        }

        public static PublishingConfiguration GetProviderWithExceptionOnly()
        {
            var section = GetSectionExample("exceptionProvider");
            return new ConfigurationSectionHandler().Create(section);
        }

        public static PublishingConfiguration GetProviderWithExceptionWrongConfigPath()
        {
            var section = GetSectionExample("exceptionProviderWrongConfig");
            return new ConfigurationSectionHandler().Create(section);
        }

        public static PublishingConfiguration GetProviderWithExceptionWithMissingHandlerSettings()
        {
            var section = GetSectionExample("exceptionProviderMissingSetting");
            return new ConfigurationSectionHandler().Create(section);
        }

        public static PublishingConfiguration GetNullProvider()
        {
            var section = GetSectionExample("nullProvider");
            return new ConfigurationSectionHandler().Create(section);
        }

        public static PublishingConfiguration GetDefaultProvider()
        {
            var section = GetSectionExample("defaultProvider");
            return new ConfigurationSectionHandler().Create(section);
        }

        public static PublishingConfiguration GetMissingProvider()
        {
            var section = GetSectionExample("missingProvider");
            return new ConfigurationSectionHandler().Create(section);
        }

        public static PublishingConfiguration GetWrongProviderType()
        {
            var section = GetSectionExample("wrongProviderType");
            return new ConfigurationSectionHandler().Create(section);
        }

        public static PublishingConfiguration GetMissingHandler()
        {
            var section = GetSectionExample("missingHandler");
            return new ConfigurationSectionHandler().Create(section);
        }
        public static PublishingConfiguration GetIncorrectHandler()
        {
            var section = GetSectionExample("incorrectHandler");
            return new ConfigurationSectionHandler().Create(section);
        }
        public static PublishingConfiguration GetBrokenProvider()
        {
            var section = GetSectionExample("brokenProvider");
            return new ConfigurationSectionHandler().Create(section);
        }
        public static PublishingConfiguration GetBrokenHandler()
        {
            var section = GetSectionExample("brokenHandler");
            return new ConfigurationSectionHandler().Create(section);
        }
        public static PublishingConfiguration GetProviderWithMessageHandler()
        {
            var section = GetSectionExample("messages");
            return new ConfigurationSectionHandler().Create(section);
        }
        public static PublishingConfiguration GetProviderTypeBrokenSettings()
        {
            var section = GetSectionExample("providerTypeBroken");
            return new ConfigurationSectionHandler().Create(section);
        }
        #endregion Read Configuration

        #region section
        public static XElement GetSectionExample(string sectionName)
        {
            var path = Path.Combine(Environment.CurrentDirectory, "TestingExamples.config");
            if (!File.Exists(path))
            {
                path = Path.Combine(Environment.CurrentDirectory, "Config/TestingExamples.config");
            }
            var xdoc = XDocument.Load(path);
            return xdoc.Root
                .Elements("publishing")
                .Where(e => e.Attribute("name").Value.Equals(sectionName))
                .First();
        }

        public static XmlNode GetSectionExampleAsNode(string sectionName)
        {
            return GetXmlNode(GetSectionExample(sectionName));
        }

        public static XmlNode GetXmlNode(XElement element)
        {
            using (XmlReader xmlReader = element.CreateReader())
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlReader);
                return xmlDoc;
            }
        }
        #endregion section
    }
}
